﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.WebApi.Utils
{
    public static class XmlUtil
    {
        public static XmlElement ElementFromDocument(XmlDocument xNode, string name)
        {
            XmlElement xChild = (XmlElement)xNode.SelectSingleNode(string.Format("./{0}", name));
            if (xChild == null)
            {
                xChild = xNode.CreateElement(name);
                xNode.AppendChild(xChild);
            }
            return xChild;
        }

        public static XmlElement Element(XmlNode xNode, string name)
        {
            XmlElement xChild = (XmlElement)xNode.SelectSingleNode(string.Format("./{0}", name));
            if(xChild == null)
            {
                xChild = xNode.OwnerDocument.CreateElement(name);
                xNode.AppendChild(xChild);
            }
            return xChild;
        }

        public static void Attribute(XmlNode xNode, string name, string value)
        {
            XmlAttribute xAttribute = xNode.Attributes[name];
            if(xAttribute == null)
            {
                xAttribute = xNode.OwnerDocument.CreateAttribute(name);
                xNode.Attributes.Append(xAttribute);
            }
            xAttribute.Value = value;
        }
    }
}
